﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using A2Z.DataAccess;

namespace A2Z.Bussiness
{
    public class MatchBLL : A2Z.DataAccess.MatchDA
    {
        public MatchBLL(Mgh.Data.Sql.DataConnection cnn)
        {
            this.IConnection = cnn;
        }

        #region get

        public dataSource.Fb_Vw_MatchDataTable getEditedFMatchByID(string matchID)
        {
            if (string.IsNullOrEmpty(matchID))
            {
                addMessage("", "matchID is null/empty", 1);
                return null;
            }

            List<string> list = new List<string>(); list.Add(matchID);
            dataSource.Fb_Vw_MatchDataTable tb = base._getFMatchsByID(list);
            if (tb == null)
                return null;
            if (tb.Count == 0)
                return tb;
            if (!tb[0].Finished)
                tb.Clear();
            return tb;
        }

        public dataSource.Fb_Vw_MatchDataTable getFMatchsByTour(DateTime startDate, DateTime endDate, string tourID, string groupID)
        {
            if (string.IsNullOrEmpty(tourID))
            {
                addMessage("", "tourID is null/empty", 1);
                return null;
            }
            if (string.IsNullOrEmpty(groupID))
            {
                addMessage("", "groupID is null/empty", 1);
                return null;
            }
            if (tourID.ToLower().Equals("all"))
            {
                tourID = string.Empty;
            }
            if (groupID.ToLower().Equals("all"))
            {
                groupID = string.Empty;
            }
            startDate = startDate.AddDays(-1).AddDays(1);
            endDate = endDate.AddDays(1).AddDays(-1);
            return base._getFMatchsByTour(startDate, endDate, tourID, groupID);
        }

        #endregion

        #region insert

        public bool insertFMatch(dataSource.Fb_Vw_MatchRow row)
        {
            if (row == null)
            {
                addMessage("", "inserted data is null", 1);
                return false;
            }
            dataSource.Fb_Vw_MatchDataTable tb = new dataSource.Fb_Vw_MatchDataTable();
            dataSource.Fb_Tbl_MatchDataTable tbM = new dataSource.Fb_Tbl_MatchDataTable();
            dataSource.Fb_Tbl_MatchRow rM = tbM.NewFb_Tbl_MatchRow();
            rM.MatchID = row.MatchID;
            rM.Stadium = row.Stadium;
            rM.StartDate = row.StartDate;
            rM.Finished = false;
            tbM.AddFb_Tbl_MatchRow(rM);

            dataSource.Fb_Tbl_TeamMatchDataTable tbTM = new dataSource.Fb_Tbl_TeamMatchDataTable();
            dataSource.Fb_Tbl_TeamMatchRow rTMA = tbTM.NewFb_Tbl_TeamMatchRow();
            dataSource.Fb_Tbl_TeamMatchRow rTMB = tbTM.NewFb_Tbl_TeamMatchRow();
            rTMA.MatchID  = rTMB.MatchID= row.MatchID;

            rTMA.CTeamID = row.TeamAID;
            rTMB.CTeamID = row.TeamBID;

            rTMA.Scores = 0;
            rTMB.Scores = 0;

            rTMA.Side = 0; rTMB.Side = 1;

            tbTM.AddFb_Tbl_TeamMatchRow(rTMA);
            tbTM.AddFb_Tbl_TeamMatchRow(rTMB);
            bool isTran =false;
            beginTransaction(ref isTran);
            if (base._insertMatchs(tbM))
            {
                if (base._insertTMatchs(tbTM))
                {
                    commitTransaction(isTran);
                    return true;
                }
            }
            rollbackTransaction(isTran);
            return false;
        }

        #endregion
     
        #region update

        public bool updateFMatch(dataSource.Fb_Vw_MatchRow row)
        {
            if (row == null)
            {
                addMessage("", "updated data is null", 1);
                return false;
            }
            dataSource.Fb_Vw_MatchDataTable tb = new dataSource.Fb_Vw_MatchDataTable();
            dataSource.Fb_Tbl_MatchDataTable tbM = new dataSource.Fb_Tbl_MatchDataTable();
            dataSource.Fb_Tbl_MatchRow rM = tbM.NewFb_Tbl_MatchRow();
            rM.MatchID = row.MatchID;
            rM.Stadium = row.Stadium;
            rM.StartDate = row.StartDate;
            rM.Finished = false;
            tbM.AddFb_Tbl_MatchRow(rM);

            dataSource.Fb_Tbl_TeamMatchDataTable tbTM = new dataSource.Fb_Tbl_TeamMatchDataTable();
            dataSource.Fb_Tbl_TeamMatchRow rTMA = tbTM.NewFb_Tbl_TeamMatchRow();
            dataSource.Fb_Tbl_TeamMatchRow rTMB = tbTM.NewFb_Tbl_TeamMatchRow();
            rTMA.MatchID = rTMB.MatchID = row.MatchID;

            rTMA.CTeamID = row.TeamAID;
            rTMB.CTeamID = row.TeamBID;

            rTMA.Scores = 0;
            rTMB.Scores = 0;

            rTMA.Side = 0; rTMB.Side = 1;

            tbTM.AddFb_Tbl_TeamMatchRow(rTMA);
            tbTM.AddFb_Tbl_TeamMatchRow(rTMB);
            bool isTran = false;
            beginTransaction(ref isTran);
            if (base._updateMatchs(tbM))
            {
                if (base._updateTMatchs(tbTM))
                {
                    commitTransaction(isTran);
                    return true;
                }
            }
            rollbackTransaction(isTran);
            return false;
        }

        public bool updateScore(int scoreA, int scoreB, string matchID)
        {
            if (string.IsNullOrEmpty(matchID))
            {
                addMessage("", "matchID is null/empty", 1);
                return false;
            }
            
            dataSource.Fb_Tbl_TeamMatchDataTable tbTM = new dataSource.Fb_Tbl_TeamMatchDataTable();
            dataSource.Fb_Tbl_TeamMatchRow rTMA = tbTM.NewFb_Tbl_TeamMatchRow();
            dataSource.Fb_Tbl_TeamMatchRow rTMB = tbTM.NewFb_Tbl_TeamMatchRow();
            rTMA.MatchID = rTMB.MatchID = matchID;
            rTMA.Scores = scoreA;
            rTMB.Scores = scoreB;
            tbTM.AddFb_Tbl_TeamMatchRow(rTMA);
            tbTM.AddFb_Tbl_TeamMatchRow(rTMB);
            return base._updateTMatchWithColumns(tbTM, tbTM.ScoresColumn.ColumnName);
        }

        public bool finishMatch(int scoreA, int scoreB, string matchID)
        {
            if (string.IsNullOrEmpty(matchID))
            {
                addMessage("", "matchID is null/empty", 1);
                return false;
            }

            dataSource.Fb_Tbl_TeamMatchDataTable tbTM = new dataSource.Fb_Tbl_TeamMatchDataTable();
            dataSource.Fb_Tbl_TeamMatchRow rTMA = tbTM.NewFb_Tbl_TeamMatchRow();
            dataSource.Fb_Tbl_TeamMatchRow rTMB = tbTM.NewFb_Tbl_TeamMatchRow();
            rTMA.MatchID = rTMB.MatchID = matchID;
            rTMA.Scores = scoreA;
            rTMB.Scores = scoreB;
            tbTM.AddFb_Tbl_TeamMatchRow(rTMA);
            tbTM.AddFb_Tbl_TeamMatchRow(rTMB);

            dataSource.Fb_Tbl_MatchDataTable tbM = new dataSource.Fb_Tbl_MatchDataTable();
            dataSource.Fb_Tbl_MatchRow rM = tbM.NewFb_Tbl_MatchRow();
            rM.Finished = true;
            tbM.AddFb_Tbl_MatchRow(rM);

            bool isTran = false;
            beginTransaction(ref isTran);
            {
                if (base._updateTMatchWithColumns(tbTM, tbTM.ScoresColumn.ColumnName))
                {
                    if (base._updateMatchWithColumns(tbM, tbM.FinishedColumn.ColumnName))
                    {
                        commitTransaction(isTran);
                        return true;
                    }
                }
            }
            rollbackTransaction(isTran);
            return false;
        }

        #endregion

        #region delete
        #endregion

        #region other

        #endregion
    }
}
